<template>
    <div class="productTotalType">

        <el-card shadow="never" class="card-com">
            <div class="card-1">
                <searchPanel @refresh="getData('reset')" @changePanel="resizeWin">
                    <!-- 查询面板 -->
                    <template v-slot:search>
                        <el-form :inline="true" :model="listSearch" class="demo-form-inline" label-width="120px">
                            <el-row :gutter="20">
                                <el-col :xs="24" :sm="12" :sd="12" :lg="8" :xl="6">
                                    <el-form-item label="序号" style="width: 100%;">
                                        <NormalInput @getData="getData('search')"
                                            v-model:modelValue="listSearch.condition.id"></NormalInput>
                                    </el-form-item>
                                </el-col>
                                <el-col :xs="24" :sm="12" :sd="12" :lg="8" :xl="6">
                                    <el-form-item label="名称" style="width: 100%;">
                                        <NormalInput @getData="getData('search')"
                                            v-model:modelValue="listSearch.condition.productCategoryName"></NormalInput>
                                    </el-form-item>
                                </el-col>
                                <el-col :xs="24" :sm="12" :sd="12" :lg="8" :xl="6">
                                    <el-form-item label="编码">
                                        <NormalInput @getData="getData('search')"
                                            v-model:modelValue="listSearch.condition.productCategoryCode"></NormalInput>
                                    </el-form-item>
                                </el-col>
                                <el-col :xs="24" :sm="12" :sd="12" :lg="8" :xl="6">
                                    <el-form-item label="类型">
                                        <BlackSelect @myChange="getData('search')"
                                            v-model:select-value="listSearch.condition.productType"
                                            :options="allProductType"></BlackSelect>
                                    </el-form-item>
                                </el-col>
                                <el-col :xs="24" :sm="12" :sd="12" :lg="8" :xl="6">
                                    <el-form-item label="备注" style="width: 100%;">
                                        <NormalInput @getData="getData('search')"
                                            v-model:modelValue="listSearch.condition.remarks"></NormalInput>
                                    </el-form-item>
                                </el-col>

                            </el-row>
                            <el-row justify="end">
                                <el-button type="primary" @click="getData('search')">查询</el-button>
                                <el-button @click="getData('reset')">重置</el-button>
                            </el-row>
                        </el-form>
                    </template>
                    <!-- 字段添加 -->
                    <template v-slot:edit>
                        <el-button :icon="Plus" type="primary" @click="AddEditBtn('add')"></el-button>

                        <DropItem v-model:table-prop="tableProp"></DropItem>
                    </template>
                </searchPanel>
            </div>
            <div class="card-2">
                <OnlyTable ref="onlyTableRef" class="card-2" :tableProps="tableProp" :pageTotal="pageParams.total"
                    @get-page="handPage" :tableData="pageParams.tableData" :tableHeight="pageParams.tableHeight"
                    :loading="pageParams.loadIng">

                    <template #handle="scope">
                        <!-- v-permission="'SUPPLIER:DETAIL'" -->
                        <el-button type="primary" size="small" link @click="detailBtn(scope.row)">查看</el-button>
                        <el-button type="primary" size="small" link
                            @click="AddEditBtn('edit', scope.row)">编辑</el-button>
                        <el-button type="danger" size="small" link @click="delBtn(scope.row)">删除</el-button>
                    </template>
                </OnlyTable>
            </div>
        </el-card>
        <addAndEdit ref="addEditRef" @refresh="getData('reset')"></addAndEdit>
        <detail ref="detailRef" @refresh="getData('reset')"></detail>
    </div>
</template>

<script setup lang="ts">
import { storeToRefs } from 'pinia';
import { onMounted, ref, nextTick, onUnmounted, defineAsyncComponent } from 'vue';
import { Search, Plus, Refresh, Download } from '@element-plus/icons-vue'
import _ from "lodash"
import { allTotalProListStore } from "./pageStore"
import { useEnumStore } from "@/store/enumeration"
const { allProductType } = storeToRefs(useEnumStore())
const onlyTableRef = ref()
const { detailData, tableProp, listSearch, pageParams, addEditRef, detailRef } = storeToRefs(allTotalProListStore())
const {
    getData1, handPage, AddEditBtn, delBtn, detailBtn
} = allTotalProListStore()
const addAndEdit = defineAsyncComponent(() => import("./components/addAndEdit.vue"))
const detail = defineAsyncComponent(() => import("./components/detail.vue"))



onMounted(() => {
    getData1('reset')
    nextTick(() => {
        const dom1 = document.querySelector(".card-1") as any;
        const dom2 = document.querySelector(".card-com") as any;
        if (dom2) {
            const height = dom2.offsetHeight - dom1.offsetHeight - 70;
            pageParams.value.tableHeight = height;
        }
    })
    window.addEventListener('resize', resizeWin)
})
onUnmounted(() => {
    window.removeEventListener('resize', resizeWin)
})
const resizeWin = _.debounce(() => {
    const dom1 = document.querySelector(".card-1") as any;
    const dom2 = document.querySelector(".card-com") as any;
    if (dom2) {
        const height = dom2.offsetHeight - dom1.offsetHeight - 70;
        pageParams.value.tableHeight = height;
    }
}, 200)
const getData = (type: string) => {
    if (type == 'reset') onlyTableRef.value.initPagination()
    getData1(type)
    resizeWin()
}
</script>

<style scoped lang="scss">
.productTotalType {
    height: calc(100vh - 70px);

    .card-com {
        display: flex;
        flex-direction: column;
        // height: 100%;

        .card-2 {
            flex-grow: 1;
        }
    }
}
</style>